package com.chinapost.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.service.ResourceServiceI;
import com.chinapost.vo.Json;
import com.chinapost.vo.Resource;
import com.opensymphony.xwork2.ModelDriven;

@Action(value = "resourceAction", results = {
		@Result(name = "resourceManager", location = "/jsp/resource/resourceManager.jsp"),
		@Result(name = "resourceAdd", location = "/jsp/resource/resourceAdd.jsp"),
		@Result(name = "resourceEdit", location = "/jsp/resource/resourceEdit.jsp") })
public class ResourceAction extends BaseAction implements ModelDriven<Resource> {

	private Resource resource = new Resource();
	private ResourceServiceI resourceService;

	public ResourceServiceI getResourceService() {
		return resourceService;
	}

	@Autowired
	public void setResourceService(ResourceServiceI resourceService) {
		this.resourceService = resourceService;
	}

	@Override
	public Resource getModel() {
		return resource;
	}

	public String resourceManager() {
		return "resourceManager";
	}
	
	public String resourceAdd(){
		return "resourceAdd";
	}
	
	public String resourceEdit(){
		return "resourceEdit";
	}

	public void do_treegrid() {
		super.writeJson(resourceService.treegrid(resource));
	}
	
	public void do_combotree() {
		super.writeJson(resourceService.comboTree());
	}
	
	public void add(){
		Json j = new Json();
		try {
			resourceService.add(resource);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}
	
	public void edit(){
		Json j = new Json();
		try {
			resourceService.edit(resource);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}
	
	public void delete(){
		Json j = new Json();
		try {
			resourceService.delete(resource);
			j.setSuccess(true);
			j.setMsg("删除成功！");
			j.setObj(resource.getCid());
		} catch (Exception e) {
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}
}
